package com.chuangke.security.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.chuangke.security.service.JwtTokenManager;
import com.chuangke.security.userdetails.JwtUserDetails;

/**
 * 自定义退出处理类 返回成功
 */
@Configuration
public class CkLogoutSuccessHandler implements LogoutSuccessHandler {

    @Autowired
    private JwtTokenManager jwtTokenManager;

    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        JwtUserDetails loginUser = jwtTokenManager.getLoginUser(request);
        if (loginUser != null) {
            // 删除用户缓存记录
            jwtTokenManager.delLoginUser(loginUser.getUuid());
            // 记录用户退出日志
        }
    }
}
